import Vue from 'vue'

if (process.env.NODE_ENV === 'development') {
  Vue.config.devtools = true
} else {
  Vue.config.devtools = false
}

import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import 'umy-ui/lib/theme-chalk/index.css'// 引入pltable样式
import ElementUI from 'element-ui'

import '@/styles/element-variables.scss'

import App from './App'
import store from './store'
import router from './router'
import qupComponent from 'qup-component'
import defaultSettings from '@/settings'
import { Message } from 'element-ui'
import { openPage } from '@/utils/index'
import inputDirectives from '@/directives/inputfilter' // 全局输入框指令
import QupGridPrint from 'qup-grid-print'
Vue.use(QupGridPrint)
import '@/permission' // permission control
import 'babel-polyfill'
import VueClipboard from 'vue-clipboard2'

import { getToken } from '@/utils/auth'
import CComponents from '@/utils/CComponents'
Vue.use(CComponents)
import directive from '@/utils/directive'
Vue.use(directive)
import html2pdf from '@/utils/html2pdf'
Vue.use(html2pdf)
import { parseTime } from '@/utils'
Vue.prototype.$parseTime = parseTime
Vue.prototype.$parseTimeYMD = key => {
  return row => {
    return parseTime(row[key], '{y}-{m}-{d}')
  }
}

Vue.prototype.defaultSettings = defaultSettings
// Vue.prototype.isProduction = true
Vue.prototype.isProduction = defaultSettings.isProduction
Vue.prototype.singleJudge = (array, warnTxt, single = false) => {
  if (array.length < 1) {
    Message.warning(`请选择【${warnTxt}】项`)
    return false
  }
  if (single && array.length > 1) {
    Message.warning(`该操作仅可单选`)
    return false
  }
  return true
}
Vue.prototype.openPage = openPage
Vue.prototype.VUE_APP_IMG_PREFIX = process.env.VUE_APP_IMG_PREFIX
// 刷新token
Vue.prototype.refreshToken = (extraData = {}, callBack) => {
  if (callBack) {
    const url = location.pathname
    extraData = { ...extraData, ...{ url }}
  }
  window.parent.postMessage({
    flag: 'refreshToken',
    extraData
  }, '*')
  window.resetToken = () => {
    callBack && callBack()
    if (store !== undefined) {
      store.commit('user/SET_TOKEN', getToken())
    }
  }
}
Vue.use(VueClipboard)
Vue.use(ElementUI, { size: 'mini' })
Vue.use(qupComponent)
Vue.use(inputDirectives) // 过滤输入字符

Vue.config.productionTip = false

Vue.config.devtools = true
Vue.filter('filterURL', function(url) {
  // url 为固定的参数 如果url为相对地址,则返回绝对地址
  if (url) {
    const isHttps = url.indexOf('https://')
    const isHttp = url.indexOf('http://')

    if (isHttps > -1 || isHttp > -1) {
      return url
    } else {
      return `${process.env.VUE_APP_IMG_PREFIX}${url}`
    }
  }
})

import exception from '@/api/exception'
Vue.use(exception)

new Vue({
  el: '#app',
  router,
  store,
  created() {
    store.dispatch('user/getInfo')
  },
  render: h => h(App)
})
